<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习5</title>
</head>

<body>

</body>
<script>
    //1.截取字符串"我爱中华人民共和国"，中的"中华"
    let str = "我爱中华人民共和国";
    let result = str.substr(2, 2);
    console.log(result);

    //2."abcoefoxyozzopp"查找字符串中所有o出现的位置
    let str1 = "abcoefoxyozzopp";
    let position = [];
    let index = str1.indexOf('o');

    while (index != -1) {
        position.push(index);
        index = str1.indexOf('o', index + 1);
    }

    console.log(position);

    //3.把字符串中所有的o替换成!
    let str2 = "abcoefoxyozzopp";
    let newStr = str2.replace(/o/g, '!');
    console.log(newStr);

    //4.判断一个字符串中出现次数最多的字符，统计这个次数
    function findChar(str) {
        let maxChar = '';
        let maxCount = 0;

        for (let i = 0; i < str.length; i++) {
            let currentCount = 0;

            for (let j = 0; j < str.length; j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    currentCount++;
                }
            }

            if (currentCount > maxCount) {
                maxCount = currentCount;
                maxChar = str.charAt(i);
            }
        }

        return { character: maxChar, count: maxCount };
    }

    let result1 = findChar("abcoefoxyozzopp");
    console.log(result1);
</script>

</html>